// ==UserScript==
// @name           	Kebrian's Tooltips
// @namespace      	http://www.rydzyk.pl/
// @include        	http://*.plemiona.pl/*
// @exclude			http://*.plemiona.pl/*screen=map*
// @require        	http://code.jquery.com/jquery-1.5.1.min.js
// @version        	1.2
// ==/UserScript==

var world = window.location.host;
world = world.split('.');
world =world[0];

$('<div id="kebrians_tooltip" style="display: none; background: #F4E4BC; color: black; position: absolute; top: 0; left: 0; z-index: 9999;">Wczytywanie...</div>').appendTo('#ds_body');
$(document).mousemove(function(ev){
	$('#kebrians_tooltip').css({
		top: ev.pageY+16,
		left: ev.pageX+16
	});
});

$('a').mouseover(function(){
	var href = $(this).attr('href');
	var variables = hrefToArray(href);
	if(variables['screen']=='player' || variables['screen']=='memberlist' || variables['screen']=='village') {
		$(this).attr('title','');
		$('#kebrians_tooltip').html('<th>Wczytywanie...</th>');
		$('#kebrians_tooltip').css('display', 'block');
		GM_xmlhttpRequest({
			method: "GET",
			url: 'http://'+world+'.plemionamapa.pl/data.php?id='+variables['id']+'&type='+variables['screen'],
			onload: function(response) {
				var data = JSON.parse(response.responseText);
				if($.isEmptyObject(data.villagedata) && $.isEmptyObject(data.playerdata) && $.isEmptyObject(data.tribedata)) {
					$('#kebrians_tooltip').html('<div style="border: 1px solid red; color: red; padding: 4px 4px 4px 4px;">Brak danych.</div>');
				}
				else {
					var code = '<table>';
					if(variables['screen']=='player') {
						code += playerinfo(variables['id'],data); // Informacje o graczu                     
					}
					if(variables['screen']=='memberlist') {
						code += tribeinfo(variables['id'],data); // Informacje o plemieniu
					}
					if(variables['screen']=='village') {
						code += villageinfo(variables['id'],data); // Informacje o wiosce
					}
					code += '</table>';

					$('#kebrians_tooltip').html(code);
				}
			}
		});
	}
});

$('a').mouseout(function(){
	$('#kebrians_tooltip').css('display', 'none');
});

var playerinfo = function(id,data) {
	var code = '';
  
	// Ostatnia aktywność gracza
	var date = new Date();
	var last_activity = (date.getTime()/1000-Math.max(data.playerdata[id][4],data.playerdata[id][6]))/60;
	last_activity = last_activity-last_activity%1;
	var minutes = last_activity%60;
	last_activity = last_activity-last_activity%60;
	var hours = last_activity/60;
	if(hours>=1)
		last_activity = hours+'h '+minutes+'m';
	else
		last_activity = minutes+'m';

	// Tabela z informacjami o graczu
	code += '<tr><th width="100px">Gracz</th><th>'+data.playerdata[id][2]+'</th></tr>';
	code += '<tr><td>Punkty:</td><td>'+data.playerdata[id][0]+'</td></tr>';
	code += '<tr><td>Wioski:</td><td>'+data.playerdata[id][3]+'</td></tr>';
	code += '<tr><td>Ranking:</td><td>'+data.playerdata[id][8]+'</td></tr>';
	code += '<tr><td>Pkt. agresora:</td><td>'+data.playerdata[id][9]+'</td></tr>';
	code += '<tr><td>Pkt. obroncy:</td><td>'+data.playerdata[id][10]+'</td></tr>';
	code += '<tr><td>Ostatnia aktywność:</td><td>'+last_activity+'</td></tr>';
  
	// Wczytanie informacji o plemieniu jeżeli gracz do jakiegoś należy
	if(data.playerdata[id][1])
		code += tribeinfo(data.playerdata[id][1],data); // Informacje o plemieniu
  
	return code;
}

var tribeinfo = function(id,data) {
	var code = '';
	
	var points_per_player = data.tribedata[id][0]/data.tribedata[id][1];
	points_per_player = points_per_player-points_per_player%1;
	
	// Tabela z informacjami o plemieniu
	var a = data.tribedata[id][5]+data.tribedata[id][6];
	code += '<th>Plemie</th><th>'+data.tribedata[id][9]+' ['+data.tribedata[id][2]+']</th></tr>';
	code += '<tr><td>Liczba czlonków:</td><td>'+data.tribedata[id][1]+'</td></tr>';
	code += '<tr><td>Punkty 40 najlepszych graczy:</td><td>'+data.tribedata[id][3]+'</td></tr>';
	code += '<tr><td>Liczba punktów:</td><td>'+data.tribedata[id][0]+'</td></tr>';
	code += '<tr><td>Średnia punktów:</td><td>'+points_per_player+'</td></tr>';
	code += '<tr><td>Ranking:</td><td>'+data.tribedata[id][4]+'</td></tr>';
	code += '<tr><td>Pokonani przeciwnicy:</td><td>'+a+'</td></tr>';
  
	return code;
}

var villageinfo = function(id,data) {
	var code = '';
	
	// Tabela z inormacami o wiosce
	$.each(data.villagedata, function(index,village){
		code += '<th>Wioska</th><th>'+village[3]+'</th></tr>';
		code += '<tr><td>Punkty:</td><td>'+village[2]+'</td></tr>';
		
		// Wczytywanie informacji o graczu jeżeli wioska do kogoś należy
		if(village[1])
			code += playerinfo(village[1],data);
	});
	
	return code;
}

var hrefToArray = function(href) {
	var data = new Array();
	href = href.split('?');
	href = href[1].split('&');
	$.each(href,function(index,value){
		value = value.split('=');
		data[value[0]] = value[1];
	});
	switch(data['screen']) {
		case 'info_player':
			data['screen'] = 'player';
			break;
		case 'info_village':
			data['screen'] = 'village';
			break;
		case 'info_ally':
			data['screen'] = 'memberlist';
			break;
	}
	return data;
}